/* ======================================  表单数据行操作  ====================================== */
const getTreeLabel = (data, idStr, nameStr) => {
	const { $fn } = window
	let stack = []
	const deep = data =>{
		data.forEach( v => {
			if( $fn.hasArray(v.children) ){
				deep( v.children )
			}else{
				stack.push({ [nameStr]:v[nameStr], [idStr]:v[idStr] })
			}
		})
	}
	deep(data)
	return stack
}
const Index = {
	// 获取 form 数据的字段数据
	_getRow(config, name, attr='name'){
		let data = []
		
		if( this.isArray( config )){
			data = config
		}else if(this.isObject( config )){
			data = config?.form ? config?.form?.data : config.data
		}
		const arr = this.hasArray( data ) ? data.filter( v => this.hasObject(v) && v[attr] === name) : []
		if(this.hasArray(arr)){
			return arr[0]
		}else{
			console.log( name + '不存在' );
			return {}
		}
	},
	getRow(config, name){ return this._getRow(config, name) },
	getGroup(config, name){ return this._getRow(config, name,'group') },
	_setRows(config, name, target, value, attr='name'){
		let data = []
		
		if( this.isArray( config )){
			data = config
		}else if(this.isObject( config )){
			data = config?.form ? config?.form?.data : config.data
		}
		
		if(this.hasArray(name)){
			data.forEach((v,i)=>{
				if( this.hasObject(v) && name.includes(v[attr])){
					data[i][target] = value
				}
			})
		}else{
			this.getRow(config, name)[target] = value
		}
	},
	setRows(config, name, target, value){ this._setRows(config, name, target, value) },
	hideRows(config, name, value){ this._setRows(config, name, 'hide', value) },
	disabledRows(config, name, value){ this._setRows(config, name, 'disabled', value) },
	getCurrentRow( data, value, idStr=window.$config.idStr ){
		const arr = data.filter( v=> this.hasObject(v) && v[idStr] === value )
		return arr.length > 0 ? arr[0] : {}
	},
	hideGroups(config, name, value){ this._setRows(config, name, 'hide', value, 'group') },
	// disabledGroups(config, name, value){ this._setRows(config, name, 'disabled', value, 'group') },
	// 获取不同格式的表单数据
	getFormFormat({ label, name, value, row={}, group, idStr, nameStr, data, uploadData, form, submit, sname, formType}){
		let backValue = {}
		let labelValue = { }
		let text = null
		let url = null
		let d = {}
		let rowName = null
		
		if(this.hasArray(data) && ( formType ==='radio' || ( formType ==='select' && !group ) )  ){
			row = this.getCurrentRow(data, value, idStr, nameStr)
			rowName = row[nameStr]
			if( sname && submit ){
				submit[sname] = rowName
			}
		}
	
		if(form){
			
			if(['select','radio'].includes(formType)){ text = row[nameStr] }
			
			if(['tags','checkbox-group'].includes(formType)){
				if(this.hasArray(data)){
					text = data.map(v=>v[nameStr])
				}
			}
			
			if(['tree-select'].includes(formType)){
				if(this.hasArray(data)){
					const d = getTreeLabel(data,  idStr, nameStr )
					text = []
					if(this.hasArray(value)){
						value.forEach( v => {
							d.forEach(m=>{
								if( v === m[idStr] ){
									text.push( m[nameStr] )
								}
							})
						})
					}else{
						d.forEach(m=>{
							if( value === m[idStr] ){
								text = m[nameStr]
							}
						})
					}
				}
			}
			
			if(['text'].includes(formType)){ 
				text = value
			}
			
			if(['upload'].includes(formType)){
				text = uploadData.fileName
				url = uploadData.url
				d = { data: uploadData }
			}
			if( label ){
				label = label.includes('请') ? label.replace('请','') : label
			}
			
			backValue = url ? { label, text, url } : { label, text }
			labelValue = { label:text, value }
			
			if(this.isObject(form)){
				form[name] = backValue
			}
			return { label, value, row, name, labelValue, backValue, model: name ? {[name]: value} : {}, ...d }
		}else{
			return { label, value, row, name, rowName, uploadData, model: name ? {[name]: value} : {} }
		}
	},
	// 表单元素加载数据
	async ajax({setAjax, setLoading, data, api, cache, param, setName, paging, setData }){
		const { $http } = window
		let id
		let way = 'pull'
		if( this.isString( param ) || this.isNumber( param ) ){
			id = param
		}else{
			if( paging ){
				param = { page:1, size: 10000, ...param }
				way = 'paging'
			}
		}
		if(api){
			if( cache ){
				let d = await this.indexDB(api)
				if(this.isArray(d)){
					if(setName){ d = d.map((v,i) => setName(v,i) ) }
					setData ? setAjax?.( setData(d) ) : setAjax?.( d )
					return d
				}
			}
			const loading = setLoading ? { onLoading: v=> setLoading(v) } : null
			let d = await $http[way](null ,api, { id, param, ...loading })
			
			d = ( way === 'paging' ? d?.data : d ) ?? []
			
			if( cache ){ this.indexDB(api, d) }
			
			if(setName && Array.isArray(d)){ d = d.map((v,i) => setName(v,i) ) }
			
			setData ? setAjax?.( setData(d) ) : setAjax?.( d )
			
			return d
		}else{
			return data
		}
	},
	// 格式化地址参数
	area_submit( param, name='area' ){
		let opt = {
			f1: 'province',
			f2: 'city',
			f3: 'country',
			f4: 'street'
		}
		let { f1, f2, f3, f4 } = opt;
		
		if( name !== 'area' ){
			f1 = name + '_' + f1
			f2 = name + '_' + f2
			f3 = name + '_' + f3
			f4 = name + '_' + f4
		}
		
		let area = param?.[name];
		if( area == null ){
			let obj = {
				[f1 + '_txt']: null,
				[f2 + '_txt']: null,
				[f3 + '_txt']: null,
				[f4 + '_txt']: null,
				[f1 + '_id']: null,
				[f2 + '_id']: null,
				[f3 + '_id']: null,
				[f4 + '_id']: null
			}
			return obj
		}
		if( window.$fn.hasArray(area) && window.$fn.isObject( area[0] ) ){
			let obj = { 
				[f1 + '_txt']: area?.[0]?.label ?? null,
				[f2 + '_txt']: area?.[1]?.label ?? null,
				[f3 + '_txt']: area?.[2]?.label ?? null,
				[f4 + '_txt']: area?.[3]?.label ?? null,
				[f1 + '_id']: area?.[0]?.value ?? null,
				[f2 + '_id']: area?.[1]?.value ?? null,
				[f3 + '_id']: area?.[2]?.value ?? null,
				[f4 + '_id']: area?.[3]?.value ?? null
			}
			return { ...obj, [name]:null }
		}else{
			return { [name]:null }
		}
	},
	area_back( param, name='area'){
		const opt = {
			f1: 'province',
			f2: 'city',
			f3: 'country',
			f4: 'street'
		}
		let { f1, f2, f3, f4 } = opt;
		if( name !== 'area' ){
			f1 = name + '_' + f1
			f2 = name + '_' + f2
			f3 = name + '_' + f3
			f4 = name + '_' + f4
		}
		const arr = []
		const p1 = param?.[f1+'_id'];
		const p2 = param?.[f2+'_id'];
		const p3 = param?.[f3+'_id'];
		const p4 = param?.[f4+'_id'];
		if(p1){ arr[0] = p1 }
		if(p2){ arr[1] = p2 }
		if(p3){ arr[2] = p3 }
		if(p4){ arr[3] = p4 }
		return { [name]: arr }
	},
	getSubtxtParam(data, param){
		let name = []
		let rs = {}
		data.forEach(v=>{
			if(v && v.subtxt && !v.hide) name.push(v.name)
		})
		
		if( name.length === 1) rs = this.select_param(param, name[0])
		else if( name.length > 1 ) rs = this.select_param(param, name)
		
		
		return { ...param, ...rs }
	},
	select_param(param, name){
		let rs = {}
		if( window.$fn.hasArray( name ) ){
			name.forEach(v=>{
				let d = param?.[v]
				let txt = v + '_txt'
				if( window.$fn.isObject( d ) ){
					rs =  d ? { [v]: d?.value, [txt]: d?.children?.trim?.(), ...rs } : null
				}else{
					rs = window.$fn.isNotEmpty( d ) ? null : { [name]:null, [txt]:null }
				}
			})
		} else if( window.$fn.isString( name ) ){
			let d = param?.[name]
			let txt = name + '_txt'
			
			if( window.$fn.isObject( d ) ){
				rs =  d ? { [name]: d?.value, [txt]: d?.children?.trim?.() } : null
			}else{
				rs = window.$fn.isNotEmpty( d ) ? null : { [name]:null, [txt]:null }
			}
		}
		return rs
	},
	tags_submit(param, name='tags'){
		let tags = param?.[name]
		if(tags){
			return { [name]: tags.split(' ') }
		}else{
			return { [name]:null }
		}
	},
	tags_back(param, name='tags'){
		let tags = param?.[name]
		if(Array.isArray(tags)){
			return { [name]: tags.join(' ') }
		}
	},
	async getCache(setAjax, api){
		if(api){
			let data = await window.$fn.indexDB(api)
			if( window.$fn.hasArray(data) ){
				setAjax(data)
			}else{
				window.$http.pull(null, api).then(data=>{
					window.$fn.indexDB(api, data)
					setAjax(data)
				})
			}
		}
	}
}

export default Index